package com.xcity.game.currency.impl;

import com.xcity.db.entity.Player;
import com.xcity.game.common.LogCause;
import com.xcity.game.currency.CurrencyType;
import com.xcity.game.currency.LongCurrency;
import com.xcity.game.event.GameEvents;
import com.xcity.game.io.DeserializeStream;
import com.xcity.pomelo.push.SyncPush;

import naga.x.App;

public class Gold extends LongCurrency {

	public Gold(DeserializeStream stream) {
		super(CurrencyType.GOLD,
				SyncPush.SYNC_GOLD,
				"[ADDGOLD]ID[{}]OLD[{}]NEW[{}]CAUSE[{}]",
				"[DECGOLD]ID[{}]OLD[{}]NEW[{}]CAUSE[{}]",
				stream);
	}
	
	@Override
	public void init(Player player) {
		super.init(player);
		this.value = player.getGold();
	}

	@Override
	protected boolean doAdd(long val, String causeStr, LogCause causeObj) {
		if (super.doAdd(val, causeStr, causeObj)) {
			owner.setGold(this.value);
			App.getApp().getEventManager().addEvent(GameEvents.EVENT_GOLD_CHANGED, owner, val, causeObj != null ? causeObj : causeStr);
			return true;
		}
		return false;
	}

	@Override
	protected boolean doDec(long val, String causeStr, LogCause causeObj) {
		if (super.doDec(val, causeStr, causeObj)) {
			owner.setGold(this.value);
			App.getApp().getEventManager().addEvent(GameEvents.EVENT_GOLD_CHANGED, owner, -val, causeObj != null ? causeObj : causeStr);
			return true;
		}
		return false;
	}
	
}
